﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms.DataVisualization.Charting;

namespace PCCardN.Pages
{
    public partial class Frm_Index : Form
    {
        public Frm_Index()
        {
           
            InitializeComponent();
            chart_User.Visible = true;
            tableLayoutP_User.Visible = true;
            bindChartBar_User();
        }
        private void bindChartBar_User()
        {
            double[] value_Bar;
            double[] value_Pie;
            var values = ConfigurationManager.AppSettings["SqlConnString"];
            using (SqlConnection conn = new SqlConnection(values))
            {

                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select * from Card_Table";
                cmd.Connection = conn;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                
                adapter.Fill(dt);
                DataTable dt1 = new DataTable();
                cmd.CommandText = "SELECT(SELECT COUNT(1) FROM Card_Table WHERE IFPrinted = '0') AS '未打印',(SELECT COUNT(1) FROM Card_Table WHERE IFPrinted = '1') AS '已打印',(select count(1) from Card_Table where CardID is not null) as '已绑卡',(select count(1)from Card_Table where [IFGranted] = 1) as '已发卡',(select count(1) from Card_Table where DATEDIFF(day,[GrantedDT], GETDATE()) = 0)as '当天发放'";
                adapter.Fill(dt1);
                double UserAllCount = dt.Rows.Count;
                if (dt.Rows.Count > 0)
                {
                    
                    double CardValidCount = double.Parse(dt1.Rows[0]["未打印"].ToString());
                    double CardPrintCount = double.Parse(dt1.Rows[0]["已打印"].ToString());
                    double CardWriteCount = double.Parse(dt1.Rows[0]["已绑卡"].ToString());
                    double CardGranted = double.Parse(dt1.Rows[0]["已发卡"].ToString());
                    double CardDaysCount = double.Parse(dt1.Rows[0]["当天发放"].ToString());
                    //各证件类型
                    //double card1 = Convert.ToDouble(dt.Rows[0]["card1"]);
                    //double card2 = Convert.ToDouble(dt.Rows[0]["card2"]);
                    //double card3 = Convert.ToDouble(dt.Rows[0]["card3"]);
                    //double card4 = Convert.ToDouble(dt.Rows[0]["card4"]);
                    //绘制柱状图
                    value_Bar = new double[] { UserAllCount, CardValidCount, CardPrintCount, CardWriteCount, CardGranted, CardDaysCount };    //要显示的数据
                    //value_Pie = new double[] { card1, card2, card3, card4 };    //要显示的数据
                }
                else
                {
                    //绘制柱状图
                    value_Bar = new double[] { 100, 50,60, 70, 80 ,10};    //要显示的数据
                    value_Pie = new double[] { 0, 0, 0, 0 };    //要显示的数据
                }
                string[] xValue_Bar = { "证卡数量", "待打印证件", "打印完成", "绑卡完成", "发放完成" ,"今日发放"};
                //显示柱状图
                showChartBar_User(this.chart_User, xValue_Bar, value_Bar);
                ShowUserCardList(value_Bar, UserAllCount);
            }
        }
        private void ShowUserCardList(double[] value,double dou)
        {
            try
            {
                txtUserCount1.Text = value[0].ToString();
                txtUserCount2.Text = value[1].ToString();
                txtUserCount3.Text = value[2].ToString();
                txtUserCount4.Text = (dou-value[3]).ToString();
                txtUserCount5.Text = value[4].ToString();
                txtUserCount6.Text = value[5].ToString();
            }
            catch { }
        }
        private void showChartBar_User(Chart chart, string[] xValue, double[] value)
        {
            //获取要显示的值     
            double[] yValue = value;
            //设置图表边框为浮雕效果 
            chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            //设置图表边框为实线  
            chart.BorderlineDashStyle = ChartDashStyle.Solid;
            //设置图表边框的宽度   
            chart.BorderlineWidth = 1;
            //设置图表类型为柱状图    
            chart.Series[0].ChartType = SeriesChartType.Column;
            //设置饼图的参数        
            chart.Series[0].CustomProperties = "BarDrawingStyle=Concave, CollectedLabel=Other, MinimumRelative" + "BarSize=10";
            chart.Series[0].Points.DataBindXY(xValue, yValue);
            //将数据绑定到图表       
        }

        private void Frm_Index_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
